using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Android.Util;


namespace Moogle
{
    /// <summary>
    /// PopTime List Adapter class. This class extends the Android.BaseAdapter class, which can 
    /// be used to display the contents of a collection (List) of PopTime objects.  Its
    /// target consumer is the ActivityPopTimeEdit class.
    /// </summary>
    public class MedicationListAdapter : BaseAdapter<Medication>
    {

        #region Local Memory
        /// <summary>
        /// Constant using to identify (filter) entries created in the
        /// Android Log by this class.
        /// </summary>
        private const string TAG = "MedicationListAdapter";
        Activity context;

        /// <summary>
        /// Flag set by the constructor, which controls whether
        /// all checkboxes will be checked by default.
        /// </summary>
        private Boolean __M_booleanAllselected;

        public List<Medication> Medications;
        /// <summary>
        /// Secondary list used to store the "selected" medications.
        /// </summary>
        private List<String> __M_colMedicationNameSelections = new List<String>();
        #endregion
        #region Class Constructor
        /// <summary>
        /// Class Constructor.
        /// </summary>
        /// <param name="context">Activity context instance</param>
        /// <param name="medications">List collection containing all Medications to be displayed by the adapter</param>
        public MedicationListAdapter(Activity context, List<Medication> medications, Boolean selected)
            : base()
        {
            this.context = context;
            this.Medications = medications;
            this.__M_booleanAllselected = selected;
            Log.Debug(TAG, "Medication List Adapter created.");
         
        }
        #endregion

        #region Base Class Overrides
        public override int Count
        {
            get { return this.Medications.Count; }
        }

        public override Medication this[int position]
        {
            get { return this.Medications[position]; }
        }
        /// <summary>
        /// GetView Override.
        /// </summary>
        /// <param name="position">integer value defining the item location in the list</param>
        /// <param name="convertView">view object instance</param>
        /// <param name="parent">view group object instance</param>
        /// <returns></returns>
        public override View GetView(int position, View convertView, ViewGroup parent)
        {
            //Get our object for this position
            var item = this.Medications[position];

            /// Try to reuse convertView if it's not  null, otherwise inflate it from our item layout
            /// This gives us some performance gains by not always inflating a new view
            /// This will sound familiar to MonoTouch developers with UITableViewCell.DequeueReusableCell()
            var view = convertView;

            if (convertView == null || !(convertView is LinearLayout))
                view = context.LayoutInflater.Inflate(Resource.Layout.list_item_checkbox, parent, false);

            /// Find references to each subview in the list item's view
            var checkbox = view.FindViewById(Resource.Id.checkBoxItem) as Android.Widget.CheckBox;

            /// Assign this item's values to the various subviews
            checkbox.SetText(item.Name, TextView.BufferType.Normal);

            /// If the AllSelected flag was set when the Adapter
            /// was initialized, then add each item to the selected 
            /// selected medication list
            if (this.__M_booleanAllselected && 
                !this.__M_colMedicationNameSelections.Contains(item.Name))
                this.__M_colMedicationNameSelections.Add(item.Name);
            
            
            Log.Debug(TAG, "Adding Checked Change Event Listener...");
            /// Wire the checkbox change listener to te checkbox control
            checkbox.Click += delegate
                {
                    Log.Debug(TAG, "Checkbox Clicked!");
                    Log.Debug(TAG, "checkbox.Checked.ToString()="+checkbox.Checked.ToString());
                    Log.Debug(TAG, "checkbox.Text = " + checkbox.Text);
                    if (checkbox.Checked)
                    {
                        if (!this.__M_colMedicationNameSelections.Contains(checkbox.Text.Trim()))
                            this.__M_colMedicationNameSelections.Add(checkbox.Text.Trim());

                    } // end if
                    else
                    {
                        if (this.__M_colMedicationNameSelections.Contains(checkbox.Text.Trim()))
                            this.__M_colMedicationNameSelections.Remove(checkbox.Text.Trim());

                    } // end else
            }; // end delegate

            
            /// Finally return the view
            return view;
        }

        public override long GetItemId(int position)
        {
            return position;
        }
        #endregion
        #region Selections Accessor
        /// <summary>
        /// Accessor that provides access to the selected medication names
        /// list maintained by the adaptor. This list contains
        /// the names of all medications that the user
        /// has selected (or checked).
        /// </summary>
        public List<String> MedicationNameSelections
        {
            get
            {
                if (null != this.__M_colMedicationNameSelections)
                    return this.__M_colMedicationNameSelections;
                else
                    return new List<String>();
            }
        }
        #endregion
    }

}